﻿using GrapeCity.Forguncy.CellTypes;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Vant.CellTypes;

namespace Vant.WpfControls.DrawingObject
{
    /// <summary>
    /// Interaction logic for HorizontalStepDrawingObject.xaml
    /// </summary>
    public partial class VerticalStepDrawingObject : UserControl
    {
        public VerticalStepDrawingObject(Steps steps, ICellInfo cell, IDrawingHelper drawingHelper)
        {
            var items = steps.options.Select(i => new StepModel()
            {
                title = i.title,
                value = i.value,
            }).ToList();
            if (items.Count == 0)
            {
                for (int i = 0; i < 3; i++)
                {
                    items.Add(new StepModel()
                    {
                        title = "步骤" + (i + 1),
                        value = (i + 1).ToString(),
                    });
                }
            }
            var currentStepIndex = items.Select(i => i.value?.ToString()).ToList().IndexOf(cell.Value?.ToString());

            currentStepIndex = CreateViewModel(steps, cell, drawingHelper, items, currentStepIndex);
            InitializeComponent();
        }
        public VerticalStepDrawingObject(Timeline timeline, ICellInfo cell, IDrawingHelper drawingHelper)
        {
            var items = timeline.options.Select(i => {
                string timestamp = i.timestamp;
                if (!string.IsNullOrEmpty(timestamp))
                {
                    try
                    {
                        timestamp = Convert.ToDateTime(timestamp).ToString(timeline.format);
                    }
                    catch (Exception)
                    {
                        timestamp = i.timestamp;
                    }
                }
                return new StepModel()
                {
                    title = timeline.placement == "bottom" ? i.title + "\r\n" + timestamp : timestamp + "\r\n" + i.title,
                    value = i.value,
                };
            }).ToList();
            if (items.Count == 0)
            {
                for (int i = 0; i < 3; i++)
                {
                    items.Add(new StepModel()
                    {
                        title = "步骤" + (i + 1),
                        value = (i + 1).ToString(),
                    });
                }
            }
            var currentStepIndex = items.Select(i => i.value?.ToString()).ToList().IndexOf(cell.Value?.ToString());

            currentStepIndex = CreateViewModel(timeline, cell, drawingHelper, items, currentStepIndex);
            InitializeComponent();
        }

        private int CreateViewModel(IStepBase steps, ICellInfo cell, IDrawingHelper drawingHelper, List<StepModel> items, int currentStepIndex)
        {
            var viewModel = new VerticalStepDrawingObjectViewModel();
            var width = drawingHelper.GetImageCellSize().Width;
            width -= 20;
            var itemWith = width / (items.Count - 1);

            if (currentStepIndex == -1 && !string.IsNullOrEmpty(cell.Value?.ToString()))
            {
                currentStepIndex = items.Count;
            }

            for (int i = 0; i < items.Count; i++)
            {
                bool isFirst = i == 0;
                bool isLast = i == items.Count - 1;

                var item = items[i];
                var itemVM = new StepItemViewModel()
                {
                    NodeText = item.title,
                };
                if (isFirst)
                {
                    itemVM.TopBorderVisibility = Visibility.Collapsed;
                }
                else if (isLast)
                {
                    itemVM.BottomBorderVisibility = Visibility.Collapsed;
                    itemVM.BorderThickness = new Thickness(0, 0, 0, 0);
                }

                if (i == currentStepIndex)
                {
                    itemVM.Icon = IconHelper.GetIcon(steps.activeIcon, drawingHelper) ?? IconHelper.GetBuiltInIcon("circleCheckFull", drawingHelper, steps.activeColor);
                    itemVM.TextColor = drawingHelper.GetBrush(steps.activeColor);
                    itemVM.StepColor = drawingHelper.GetBrush(steps.activeColor);
                    itemVM.TopBorderColor = drawingHelper.GetBrush(steps.activeColor);
                    itemVM.BottomBorderColor = drawingHelper.GetBrush(steps.inactiveColor);
                }
                else if (i < currentStepIndex)
                {
                    itemVM.Icon = IconHelper.GetIcon(steps.finishIcon, drawingHelper);
                    itemVM.TextColor = drawingHelper.GetBrush("#323233");
                    itemVM.StepColor = drawingHelper.GetBrush(steps.activeColor);
                    itemVM.TopBorderColor = drawingHelper.GetBrush(steps.activeColor);
                    itemVM.BottomBorderColor = drawingHelper.GetBrush(steps.activeColor);
                }
                else
                {
                    itemVM.Icon = IconHelper.GetIcon(steps.inactiveIcon, drawingHelper);
                    itemVM.TextColor = drawingHelper.GetBrush(steps.inactiveColor);
                    itemVM.StepColor = drawingHelper.GetBrush(steps.inactiveColor);
                    itemVM.TopBorderColor = drawingHelper.GetBrush(steps.inactiveColor);
                    itemVM.BottomBorderColor = drawingHelper.GetBrush(steps.inactiveColor);
                    itemVM.TextColor = drawingHelper.GetBrush(steps.inactiveColor);
                }
                viewModel.Items.Add(itemVM);
            }

            this.DataContext = viewModel;
            return currentStepIndex;
        }

        public class VerticalStepDrawingObjectViewModel : ModelBase
        {
            public List<StepItemViewModel> Items { get; set; } = new List<StepItemViewModel>();
        }

        public class StepModel
        {
            public string title { get; set; }
            public string value { get; set; }
        }

        public class StepItemViewModel
        {
            public string NodeText { get; set; }
            public Brush TextColor { get; set; }
            public Visibility TopBorderVisibility { get; set; }
            public Thickness TopBorderMargin { get; set; }
            public Brush TopBorderColor { get; set; }

            public Visibility BottomBorderVisibility { get; set; }
            public Thickness BottomBorderMargin { get; set; }
            public Brush BottomBorderColor { get; set; }
            public object Icon { get; set; }
            public Brush StepColor { get; set; }
            public Visibility StepIconVisibility { get { return Icon != null ? Visibility.Visible : Visibility.Collapsed; } }
            public Visibility StepDotVisibility { get { return Icon == null ? Visibility.Visible : Visibility.Collapsed; } }

            public Thickness BorderThickness { get; set; } = new Thickness(0, 0, 0, 1);
        }
    }
}
